package com.mars.pay.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 订单号生成器
 *
 * @author Mars
 */
public class OrderNoGenerator {

    private static final AtomicInteger SEQUENCE = new AtomicInteger(0);
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    /**
     * 生成订单号
     * 格式：ORDER + 时间戳(14位) + 序列号(4位)
     * 示例：ORDER20250101120530001
     *
     * @return 订单号
     */
    public static String generate() {
        String timestamp = LocalDateTime.now().format(FORMATTER);
        int seq = SEQUENCE.incrementAndGet();
        if (seq >= 10000) {
            SEQUENCE.set(0);
            seq = SEQUENCE.incrementAndGet();
        }
        return String.format("ORDER%s%04d", timestamp, seq);
    }
}

